//
//  YWElectricityMeterRecordVC.m
//  maintain
//
//  Created by Kyo on 16/2/29.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWElectricityMeterRecordVC.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "YWMeterReadingDetailRecordTableViewController.h"
#import "YWMeterReadingRecordCell.h"
#import "YWMeterReadingManager.h"
#import "MJRefresh.h"
#import "YWCommonTool.h"

@interface YWElectricityMeterRecordVC ()
@property (nonatomic, strong) NSArray* sourceDatas;
@property (nonatomic, assign) NSInteger page;
@end

@implementation YWElectricityMeterRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.tableView.rowHeight = KrowHeight;
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    self.tableView.tableFooterView = [[UIView alloc]init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)refreshTop
{
    self.page = 1;
    [self.tableView.mj_footer resetNoMoreData];
    [self requestData:YWTableViewFreshTypeDown];
}

- (void)refreshFootor
{
    [self requestData:YWTableViewFreshTypeUp];
}

- (void)requestData:(YWTableViewFreshType)refreshType{
    if (refreshType == YWTableViewFreshTypeUp) {
        self.page++;
    }
    YWWeakSelf(weakSelf);
    [[YWMeterReadingManager shareManager] requestMyMeterReadingRecordsWithEquipmentTypeOwnCode:@"Electric_Meter" page:self.page pageSize:20 Block:^(BOOL success) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (refreshType == YWTableViewFreshTypeDown) {
                [weakSelf.tableView.mj_header endRefreshing];
            }
            else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            weakSelf.sourceDatas = nil;
            [weakSelf.tableView reloadData];
        });
    }];
}
- (NSArray*)sourceDatas
{
    if (!_sourceDatas) {
        NSInteger eEquipmentTypeId = [[YWMeterReadingManager shareManager]electricMeterEquipmentTypeId];
        NSArray *electricityMeterRecord = [[NSArray alloc]initWithArray:[[YWMeterReadingManager shareManager] meterReadingRecordWithEquipmentType:eEquipmentTypeId page:self.page]];
        _sourceDatas = electricityMeterRecord;
    }
    return _sourceDatas;
}
#pragma mark - UITableViewDataSource mothods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.sourceDatas.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.sourceDatas[section] count];
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    YWMeterReadingRecordCell* cell = [YWMeterReadingRecordCell cellWithTableView:tableView];
    YWMeterReadingRecord*  meterReadingRecord = self.sourceDatas[indexPath.section][indexPath.row];
    cell.meterReadingRecord = meterReadingRecord;
    return cell;
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
    YWMeterReadingRecord*  meterReadingRecord = [self.sourceDatas[section] count]>0?self.sourceDatas[section][0]:nil;
    NSMutableString *yearMonth = [NSMutableString stringWithFormat:@"%ld",(long)meterReadingRecord.yearMonth];
    if (yearMonth.length>=4) {
        [yearMonth insertString:@"年" atIndex:4];
        [yearMonth insertString:@"月" atIndex:yearMonth.length];
    }
    return yearMonth;
}
#pragma mark - UITableViewDelegate mothods
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWMeterReadingRecord*  meterReadingRecord = self.sourceDatas[indexPath.section][indexPath.row];
    YWMeterReadingDetailRecordTableViewController* detailVC = [[YWMeterReadingDetailRecordTableViewController alloc] init];
    detailVC.meterReadingRecord = meterReadingRecord;
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
